import {Component, OnInit} from '@angular/core';
import {_HttpClient} from '@delon/theme';
import {ActivatedRoute, Router} from '@angular/router';
import {UtilityService} from '../../../../service/utils.service';
import {appConfig} from '../../../../service/common';
import {NzModalService, NzNotificationService} from 'ng-zorro-antd';
import {OperatrModule} from '../../../../service/operators';

@Component({
  selector: 'app-operator-info',
  templateUrl: './operator-info.component.html',
  styleUrls: ['./operaotr-info.component.less']
})
export class OperatorInfoComponent implements OnInit {

    constructor(
        private modal: NzModalService,
        private http: _HttpClient,
        private router: Router,
        private nznot: NzNotificationService,
        private utilityService: UtilityService,
        private activatedRoute: ActivatedRoute, // 注入路由，接收到参数
    ) { }

    operatorId: string; // 接受传过来的id
    operator: OperatrModule = new OperatrModule();
    id: string; // 传递的值

    // tabs内容
    tabs = [
        {
            name   : '角色',
            router : 'operatorInfo/operatorrole'
        },
        {
            name   : '功能权限',
            router : 'operatorInfo/operatfunc'
        },
        {
            name   : '身份',
            router : 'operatorInfo/identity'
        },
        {
            name   : '个性化配置',
            router : 'operatorInfo/operatorconfig'
        },
        {
            name   : '快捷菜单',
            router : 'operatorInfo/shortcutMenu'
        },
        {
            name   : '重组菜单',
            router : 'operatorInfo/reorganizeation'
        }

    ];
    // 布局样式
    widthLeft = 6;
    widthRight = 18;
    isShow = true;
    text= '收起'

    // 收缩方法
    toggleCollapsed(): void {
        this.isShow = !this.isShow;
        if(this.isShow === true){
            this.widthLeft = 6;
            this.widthRight = 18;
            this.text = '收起'
        }else{
            this.widthLeft = 2;
            this.widthRight = 22;
            this.text = '展开'
        }
    }

    ngOnInit() {
        this.activatedRoute.queryParams.subscribe(queryParams => {
            this.operatorId = queryParams.operatorId;
        });
        // 每次进入页面判断是否存在，存在就存入本地，不存在，就去本地取
        if(this.operatorId){
            localStorage.setItem('operatorId', this.operatorId);
        }else{
            this.operatorId = localStorage.getItem('operatorId'); // 运行环境ID本地存储
        }
        this.id = this.operatorId;
        this.router.navigate(['operatorInfo/operatorrole', this.id]); // 跳转路由
        this.getOperatorInfo(this.operatorId); // 调用方法

    }

    // 根据id查询操作员详情
    getOperatorInfo(id) {
        this.utilityService.getData(appConfig.serverUrl + appConfig.API.acOperatorsAdd + '/' + id)
            .subscribe(
                (val) => {
                    this.operator = val.result;
                }
            );
    }

    operatorStaus(type) {
        let status = {
            guid: this.operator.guid,
            operatorStatus: type
        };
        this.utilityService.putData(appConfig.serverUrl + appConfig.API.changeStatus, status)
            .subscribe(
                (val) => {
                    this.getOperatorInfo(this.operator.guid)
                    this.nznot.create('success', val.msg , val.msg);
                },
                (error) => {
                    this.nznot.create('error', error.msg , error.msg);
                }
            );
    }

    // tab切换方法--路由跳转
    getRouter(it) {
        this.router.navigate([it,this.id]);
    }

}
